package com.csun.util;


import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.util.json.JSONArray;
import org.junit.Test;

/**
 * @ClassName: ActivitiUtil.java
 * @Description:Activiti工作流工具类
 * @author danjinsong danjingsong@dragonwake.cn
 * @date 2017年2月16日下午12:21:16
 * 
 */

public class DynamicActivitiFactory {
	/**
	 * 流程引擎
	 */
	private static ProcessEngine processEngine = ProcessEngines
			.getDefaultProcessEngine();

	/**
	 * 测试建表
	 * @throws Exception 
	 */
	@Test
	public void table() throws Exception {
		ProcessEngineConfiguration
			.createProcessEngineConfigurationFromResource("activiti.cfg.xml")
			.buildProcessEngine();
//		dynamicActivitiProcessFactoryMethod(null);
	}
	/**
	 * 动态activiti 流程图工程方法
	 * @throws Exception 
	 */
	public static void dynamicActivitiProcessFactoryMethod(JSONArray parms) throws Exception{
		 // 1. Build up the model from scratch
	    BpmnModel model = new BpmnModel();
	    Process process = new Process();
	    model.addProcess(process);
	    process.setId("my-process");

	    process.addFlowElement(createStartEvent());
	    process.addFlowElement(createUserTask("task1", "请假申请", "fred"));
	    process.addFlowElement(createUserTask("task2", "Second task", "john"));
	    process.addFlowElement(createEndEvent());
	    
	    process.addFlowElement(createSequenceFlow("start", "task1"));
	    process.addFlowElement(createSequenceFlow("task1", "task2"));
	    process.addFlowElement(createSequenceFlow("task2", "end"));
	    
	    // 2. Generate graphical information
	    new BpmnAutoLayout(model).execute();
	    
	    // 3. Deploy the process to the engine
//	    Deployment deployment = 
		processEngine.getRepositoryService()
					 .createDeployment()
					 .addBpmnModel("dynamic-model.bpmn", model)
					 .name("Dynamic process deployment")
					 .deploy();
	    processEngine.getRuntimeService()
	    			 .startProcessInstanceByKey("my-process");
	    
	    // 4. Start a process instance
//	    ProcessInstance processInstance = 

	    // 5. Check if task is available
//	    List<Task> tasks = processEngine.getTaskService().createTaskQuery()
//	      .processInstanceId(processInstance.getId()).list();
	//    
//	    Assert.assertEquals(1, tasks.size());
//	    Assert.assertEquals("First task", tasks.get(0).getName());
//	    Assert.assertEquals("fred", tasks.get(0).getAssignee());
	    
	    // 6. Save process diagram to a file  
//	    InputStream processDiagram = processEngine.getRepositoryService().getProcessDiagram(processInstance.getProcessDefinitionId());
//	    FileUtils.copyInputStreamToFile(processDiagram, new File("src/dy/diagram.png"));
	    
	    // 7. Save resulting BPMN xml to a file
//	    InputStream processBpmn = processEngine.getRepositoryService().getResourceAsStream(deployment.getId(), "dynamic-model.bpmn");
//	    FileUtils.copyInputStreamToFile(processBpmn, new File("src/dy/process.bpmn"));
	}
	
	
	/**
	 *	构建 UserTask 方法 
	 * @param id
	 * @param name
	 * @param assignee
	 * @return
	 */
	private static UserTask createUserTask(String id, String name, String assignee) {
		UserTask userTask = new UserTask();
		userTask.setName(name);
		userTask.setId(id);
		userTask.setAssignee(assignee);
		return userTask;
	}
	/**
	 * 构建流程线路图方法
	 * @param from
	 * @param to
	 * @return
	 */
	private static SequenceFlow createSequenceFlow(String from, String to) {
		SequenceFlow flow = new SequenceFlow();
		flow.setSourceRef(from);
		flow.setTargetRef(to);
		return flow;
	}
	/**
	 * 构建开始事件
	 * @return
	 */
	private static StartEvent createStartEvent() {
		StartEvent startEvent = new StartEvent();
		startEvent.setId("start");
		return startEvent;
	}
	/**
	 * 构建结束事件
	 * @return
	 */
	private static EndEvent createEndEvent() {
		EndEvent endEvent = new EndEvent();
		endEvent.setId("end");
		return endEvent;
	}

}
